/**
 * 剑指 Offer 09. 用两个栈实现队列
 * https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/
 */

const elasticCount = 100;

function CQueue() {
  this.stackToDelete = new Array(elasticCount).fill(0);
  this.topDelete = -1;
  this.stackToAppend = new Array(elasticCount).fill(0);
  this.topAppend = -1;
  this.isAppend = true;
}

/**
 * @param {number} value
 * @return {void}
 */
CQueue.prototype.appendTail = function (value) {
  if (!this.isAppend) {
    this.isAppend = true;
    while (this.topDelete >= 0) {
      this.topAppend += 1;
      this.stackToAppend[this.topAppend] = this.stackToDelete[this.topDelete];
      this.topDelete -= 1;
    }
  }
  if (this.topAppend >= this.stackToAppend.length - 1) {
    this.stackToAppend = this.stackToAppend.concat(new Array(elasticCount).fill(0));
    this.stackToDelete = this.stackToDelete.concat(new Array(elasticCount).fill(0));
  }
  this.topAppend += 1;
  this.stackToAppend[this.topAppend] = value;
};

/**
 * @return {number}
 */
CQueue.prototype.deleteHead = function () {
  if (this.isAppend) {
    this.isAppend = false;
    while (this.topAppend >= 0) {
      this.topDelete += 1;
      this.stackToDelete[this.topDelete] = this.stackToAppend[this.topAppend];
      this.topAppend -= 1;
    }
  }
  if (this.topDelete < 0) {
    return -1;
  }
  const result = this.stackToDelete[this.topDelete];
  this.topDelete -= 1;
  return result;
};
